<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\helpers\traits\ImageUpload;
use app\helpers\traits\SearchDataForModel;
use app\model\Community as ModelCommunity;
use think\exception\ValidateException;
use think\Request;

class Community extends BaseController
{
    use SearchDataForModel, ImageUpload;

    /**
     * @User: 刘海龙
     * @Date: 2021/7/26
     * @Time: 10:21
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 数据列表
     */
    public function getCommunityList()
    {
        $limit = $this->request->param('limit', 0);
        $res = $this->search(new ModelCommunity(), [], $limit);
        return $this->response($res);

    }


    /***
     * @User: 刘海龙
     * @Date: 2021/7/26
     * @Time: 10:16
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 保存and编辑
     */
    public function saveCommunity()
    {
        $id = $this->request->param('id', 0);
        $data = $this->request->param();
        try {
            $validator = $this->validate($data,
                \app\validate\Community::rules($id),
                \app\validate\Community::msg());
            if ($validator) {
                if (intval($id) === 0) {
                    $community = new ModelCommunity();
                } else {
                    $community = ModelCommunity::find($id);
                }
                $community->save($data);
            }
            return $this->response();

        } catch (ValidateException $exception) {
            return $this->response(403, $exception->getError());
        }
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/7/26
     * @Time: 10:42
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 更改状态
     */
    public function setCommunityStatus()
    {
        $id = $this->request->param('id');
        $data = $this->request->param();
        $comm = ModelCommunity::find($id);
        if (is_null($comm)) {
            return $this->response(403, '数据不存在');
        }
        $comm->save($data);
        return $this->response();
    }

    /**
     * @param $id
     * @User: 刘海龙
     * @Date: 2021/7/26
     * @Time: 10:46
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 获取唯一数据
     */
    public function communityRead($id)
    {
        $comm = ModelCommunity::find($id);
        if (is_null($comm)) {
            return $this->response(403, '数据不存在');
        }
        $comm->status = (string)$comm->status;
        return $this->response($comm->toArray());
    }

    /**
     * @param $id
     * @User: 刘海龙
     * @Date: 2021/7/26
     * @Time: 10:53
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除
     */
    public function communityDelete($id)
    {
        $ids = explode(',', $id);
        foreach ($ids as $id) {

            $comm = ModelCommunity::find($id);
            if (is_null($comm)) {
                return $this->response(404, '数据不存在');
            }
            //->force()
            $comm->delete();
        }
        return $this->response();
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/7/26
     * @Time: 11:43
     * @return \think\response\Json
     * @throws \Exception
     * 导入社区名称数据
     */
    public function importExcel()
    {
        if (!$this->request->file('file')) {
            return $this->response(501, '请选择上传文件');
        }
        $file = $this->request->file('file');
        $result = $this->saveImportExcel($file, 'import_excel');
        $data = [];
        foreach ($result as $key => $value) {
            $data[] = [
                'name' => $value[0] ?? '',
            ];
        }
        $comm = new ModelCommunity();
        $comm->saveAll($data);
        return $this->response();
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/7/26
     * @Time: 11:54
     * 下载
     */
    public function downloadFile()
    {
        $file = public_path() . 'storage/template/demo.xlsx';
        $data = $this->downloadExcel($file);
        return $data;
    }


    /**
     * @User: 刘海龙
     * @Date: 2021/7/26
     * @Time: 15:54
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 社区 select 赋值
     */
    public function getSelectTypeComm(){
        $data = ModelCommunity::field('id,name')->select()->toArray();
        return $this->response($data);
    }

}
